from contextlib import closing
from flaskr_blog import app
import sqlite3, os
from flaskr_blog.config import basedir

def connect_db():
    path = os.path.join(basedir, app.config['DATABASE'])
    return sqlite3.connect(path)

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read().decode('utf-8'))
        db.commit()

if __name__ == '__main__':
    init_db()